package com.jh.web.radar;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.concurrent.ArrayBlockingQueue;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.jh.pojo.Entity;
import com.jh.pojo.RadarMsg;
import com.jh.pojo.SPInfo;
import com.jh.pojo.cmd.MulitResideCmdBean;
import com.jh.utils.BlockingQueueUtils;
import com.jh.utils.CacheUtils;

/**
 * Servlet implementation class MulitReside
 */
@WebServlet(description = "Mulitpul Reside", urlPatterns = { "/MulitResideServlet" })
public class MulitResideServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    static final Logger logger = LoggerFactory.getLogger(MulitResideServlet.class);

    BlockingQueueUtils queueUtils = BlockingQueueUtils.getInstance();

    ArrayBlockingQueue<String> txBq = queueUtils.gettxBq();

    Gson g = new Gson();

    Type jsonType;

    MulitResideCmdBean mrcBeanCmd = null;

    String ipIndex = null;

    CacheUtils cacheUtils = CacheUtils.getInstance();

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MulitResideServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset:utf-8");
        String dataJson = request.getParameter("paraJson");
        logger.debug("--->" + "dataJson: " + dataJson);
        System.out.println("--->" + "dataJson: " + dataJson);

        try {
            mrcBeanCmd = g.fromJson(dataJson, MulitResideCmdBean.class);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        System.out.println(mrcBeanCmd.toString());

        ipIndex = mrcBeanCmd.getIp();
        SPInfo spInfo = (SPInfo) cacheUtils.query(ipIndex);
        if (spInfo == null) {

            logger.debug("--->" + " spInfo == null ");
            return;
        }
        RadarMsg radarMsg = spInfo.getRadarMsg();

        if (radarMsg == null) {

            logger.debug("--->" + " radarMsg == null ");
            return;
        }
        MulitResideCmdBean mrcTmpBean = radarMsg.getrMrcBean();
        mrcTmpBean.setStartAngle(mrcBeanCmd.getStartAngle());
        mrcTmpBean.setEndAngle(mrcBeanCmd.getEndAngle());
        mrcTmpBean.setSpeed(mrcBeanCmd.getSpeed());
        mrcTmpBean.setCount(mrcBeanCmd.getCount());
        mrcTmpBean.getReside1().setAngle(mrcBeanCmd.getReside1().getAngle());
        mrcTmpBean.getReside1().setTime(mrcBeanCmd.getReside1().getTime());
        mrcTmpBean.getReside2().setAngle(mrcBeanCmd.getReside2().getAngle());
        mrcTmpBean.getReside2().setTime(mrcBeanCmd.getReside2().getTime());
        mrcTmpBean.getReside3().setAngle(mrcBeanCmd.getReside3().getAngle());
        mrcTmpBean.getReside3().setTime(mrcBeanCmd.getReside3().getTime());
        mrcTmpBean.getReside4().setAngle(mrcBeanCmd.getReside4().getAngle());
        mrcTmpBean.getReside4().setTime(mrcBeanCmd.getReside4().getTime());
        mrcTmpBean.getReside5().setAngle(mrcBeanCmd.getReside5().getAngle());
        mrcTmpBean.getReside5().setTime(mrcBeanCmd.getReside5().getTime());
        mrcTmpBean.getReside6().setAngle(mrcBeanCmd.getReside6().getAngle());
        mrcTmpBean.getReside6().setTime(mrcBeanCmd.getReside6().getTime());
        mrcTmpBean.getReside7().setAngle(mrcBeanCmd.getReside7().getAngle());
        mrcTmpBean.getReside7().setTime(mrcBeanCmd.getReside7().getTime());
        mrcTmpBean.getReside8().setAngle(mrcBeanCmd.getReside8().getAngle());
        mrcTmpBean.getReside8().setTime(mrcBeanCmd.getReside8().getTime());
        mrcTmpBean.getReside9().setAngle(mrcBeanCmd.getReside9().getAngle());
        mrcTmpBean.getReside9().setTime(mrcBeanCmd.getReside9().getTime());
        mrcTmpBean.getReside10().setAngle(mrcBeanCmd.getReside10().getAngle());
        mrcTmpBean.getReside10().setTime(mrcBeanCmd.getReside10().getTime());

        Entity entity = new Entity();
        entity.setType(13);
        entity.setData(mrcBeanCmd);
        String json = null;
        try {
            json = g.toJson(entity);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        BlockingQueueUtils queueUtils = BlockingQueueUtils.getInstance();

        ArrayBlockingQueue<String> txBq = queueUtils.gettxBq();
        try {
            txBq.put(json);
        } catch (InterruptedException e) {
            logger.error(e.getMessage(), e);
        } catch (Exception e1) {
            logger.error(e1.getMessage(), e1);
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
